# provides statistics of collected Go source code
# includes:
# - total LOC
# - # projects
# - size
# - ...

import os
from os.path import join, getsize
import sys
import re

# loc function taken from http://www.aminus.org/blogs/index.php/2007/07/09/lines_of_code
# Modified for .go files
# Rough estimate, doesn't uniformly format code or consider multi-line comments
def loc(path, pattern='^.*\.go$'):
  lines = 0
  for root, dirs, files in os.walk(path):
    for name in files:
      if re.match(pattern, name):
        f = open(os.path.join(root, name), 'rb')
        for line in f:
          line = line.strip()
          if line and not line.startswith('//'):
            lines += 1
        f.close()
  return lines

# Simple folder count
def repo_count(path, pattern='^\.(git|hg|svn)$'):
  count = 0
  for dir in os.listdir(path):
    if os.path.isdir(os.path.join(path, dir)):
      if dir.startswith('\.'):
        print dir
      count += 1
  return count

# Returns size in bytes of all Go source files
def size(path, pattern='^.*\.go$'):
  sum = 0
  for root, dirs, files in os.walk(path):
    for name in files:
      if re.match(pattern, name):
        sum += getsize(join(root, name))
  return sum
    

print 'Projs/Pkgs:\t' + str(repo_count(sys.argv[1]))
print 'KLOC (est.)\t' + str(loc(sys.argv[1])/1000.0)
print 'Size (bytes)\t' + str(size(sys.argv[1]))
        
